package util;

import java.util.ArrayList;
import java.util.List;

public class GraphUtil {
    public static int[][] parseGraph(String s) {
        String[] list = s.trim().substring(1, s.length()-1).split("],");
        int m = list.length, n = 0;
        for(int i=0; i<list.length; ++i) {
            String[] tempList = list[i].substring(1, list[i].length()).split(",");
            n = tempList.length;
            break;
        }
        int[][] result = new int[m][n];
        for(int i=0; i<m; ++i) {
            int[] nums = ListUtil.parseIntArray(i<m-1 ? list[i]+"]" : list[i]);
            for(int j=0; j<n; ++j) {
                result[i][j] = nums[j];
            }
        }
        return result;
    }
    public static char[][] parseGraphChar(String s) {
        String[] list = s.substring(1, s.length()-1).split("],");
        int m = list.length, n = 0;
        for(int i=0; i<list.length; ++i) {
            String[] tempList = list[i].substring(1, list[i].length()).split(",");
            n = tempList.length;
            break;
        }
        char[][] result = new char[m][n];
        for(int i=0; i<m; ++i) {
            char[] res = ListUtil.parseCharArray(i<m-1 ? list[i]+"]" : list[i]);
            for(int j=0; j<n; ++j) {
                result[i][j] = res[j];
            }
        }
        return result;
    }
    public static void printGraph(int[][] graph) {
        int m = graph.length, n = graph[0].length;
        for(int i=0; i<m; ++i) {
            for(int j=0; j<n; ++j) {
                System.out.print(graph[i][j] + " ");
            }
            System.out.println();
        }
    }
    public static void printGraph(char[][] graph) {
        int m = graph.length, n = graph[0].length;
        for(int i=0; i<m; ++i) {
            for(int j=0; j<n; ++j) {
                System.out.print(graph[i][j] + " ");
            }
            System.out.println();
        }
    }

    public static void main(String[] args) {
        int[][] graph = GraphUtil.parseGraph("[[1,2,2,3,5],[3,2,3,4,4],[2,4,5,3,1],[6,7,1,4,5],[5,1,1,2,4]]");
        GraphUtil.printGraph(graph);
    }
}
